package com.felix.algorithms.planning;

/**
 * MaximumSubarray
 *
 * @author xuejinhua
 * @date 2018/9/12 15:03
 */
public class MaximumSubarray {

    /**
     * Q53
     *
     * @return
     */
    public static int solution(int[] nums) {
        if (nums.length == 1) {
            return nums[0];
        }
        int[] dp = new int[nums.length];
        for (int i = 0; i < nums.length; i++) {
            for (int j = 0; j < i + 1; j++) {
                dp[i] += nums[j];
            }
        }
        int max = dp[0];
        for (int i = 0; i < dp.length; i++) {
            for (int j = i + 1; j < dp.length; j++) {
                int temp = dp[j] - (dp[i] < 0 ? dp[i] : 0);
                max = max > temp ? max : temp;
            }
        }
        return max;
    }

    public static void main(String[] args) {
        int[] nums = {1, 2};
        solution(nums);
    }
}
